package com.top10.controller;

import com.google.gson.JsonObject;
import com.top10.bean.JwtUtil;
import com.top10.bean.ResponseBean;
import com.top10.common.WebConst;
import com.top10.entity.Address;
import com.top10.service.impl.AddressServiceImpl;
import com.top10.utils.MD5;
import net.sf.json.JSON;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;


import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 *地区信息Controller
 */
@RestController
@RequestMapping("/address")
public class AddressController {

    @Autowired
    private AddressServiceImpl addressService;



    /**
     * 查询省级地区地名
     * @return22223666
     * @throws Exception
     */
    @RequestMapping(value = "/getparent",method = RequestMethod.POST)
    @ResponseBody
    public ResponseBean getAddressParent()throws Exception{
       /* String token = request.getHeader(WebConst.TOKEN);
        String temp = jwtUtil.parseJwtPayload(token);
        JSONObject obj = JSONObject.fromObject(temp);
        System.out.println(obj.getString("sub"));*/
        Integer id=addressService.getParent();
        List<Address> addresses=addressService.getParents(id);
      return   ResponseBean.createSuccess().addData("addresses",addresses);
    }

    /**
     * 查询省级以下市区地名
     * @param id
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/getson",method = RequestMethod.POST)
    @ResponseBody
    public ResponseBean getAddressSon(Integer id)throws Exception{
        if (id==null){
            return ResponseBean.createError("参数不能为空");
        }
        List<Address> addresses=addressService.getParents(id);
        return   ResponseBean.createSuccess().addData("addresses",addresses);
    }


}
